package com.hddf.project.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import org.jdom.xpath.XPath;


@SuppressWarnings({ "rawtypes" })
public class XmlUtil {
	private static O o = new O();

	/**
	 * 根据机构代码转化成中文
	 */
	public static String getAreaByCode(String code) throws IOException, JDOMException {
		try {
			SAXBuilder builder = new SAXBuilder();
			Document doc = builder.build(o.getRealPath() + "xml/provinceCascade.xml");
			Element root = doc.getRootElement();
			XPath xpath = XPath.newInstance("//*[@postcode=" + code + "]/@name");
			List list = xpath.selectNodes(root);
			Attribute attribute = (Attribute) list.get(0);
			if (null != attribute) {
				return attribute.getValue();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 格式化XML
	 */
	public static Format formatXML() {
		Format format = Format.getCompactFormat();
		format.setEncoding("utf-8");
		format.setIndent(" ");
		return format;
	}

	/**
	 * 获取数据库连接信息
	 * 
	 * @return
	 */
	public static List<String> getHibernateCfgXMl() {
		String filePath = o.getRealPath() + "/WEB-INF/classes/hibernate.cfg.xml";
		List<String> sglo = new ArrayList<String>();
		try {
			Element element;
			// 读取文件
			SAXBuilder builder = new SAXBuilder();
			builder.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
			Document document = builder.build(new FileInputStream(new File(filePath)));
			element = document.getRootElement();// 获得根节点

			List list = element.getChildren();// 取得子节点
			for (Iterator items = list.iterator(); items.hasNext();) {
				Element elementO = (Element) items.next();
				List listO = elementO.getChildren();
				for (Iterator itemsO = listO.iterator(); itemsO.hasNext();) {
					Element elementT = (Element) itemsO.next();
					sglo.add(elementT.getText());
				}
			}
			XMLOutputter output = new XMLOutputter(formatXML());
			output.output(document, new FileOutputStream(filePath));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sglo;
	}

	/**
	 * 配置读取
	 * 
	 * @return
	 */
	public static String conf(String property) {
		String rst = "";
		String filePath = o.getRealPath() + "/file/configuration/configuration.xml";
		try {
			Element element;
			// 读取文件
			SAXBuilder builder = new SAXBuilder();
			builder.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
			Document document = builder.build(new FileInputStream(new File(filePath)));
			element = document.getRootElement();// 获得根节点

			List list = element.getChildren();// 取得子节点
			root: for (Iterator items = list.iterator(); items.hasNext();) {
				Element elementO = (Element) items.next();
				List listO = elementO.getChildren();
				for (Iterator itemsO = listO.iterator(); itemsO.hasNext();) {
					Element elementT = (Element) itemsO.next();
					if (elementT.getAttributeValue("name").equals(property)) {
						rst = elementT.getText();
						break root;
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rst;
	}

}
